package com.gx.smart.building.ui.swap.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.gx.smart.base.BaseAdapter;
import com.gx.smart.building.R;
import com.gx.smart.building.databinding.ItemSwapDeviceLayoutBinding;
import com.huishi.gx.ib.grpc.lib.ib.admin.schedule.v1.RecordContent;

import java.util.List;

/**
 * @author: xiaosy
 * 时间: 2022/9/6
 * @Description: 设备运行及变更情况
 * @Version: 1.0
 **/
public class SwapDeviceAdapter extends BaseAdapter<RecordContent, SwapDeviceAdapter.ViewHolder> {
    private List<RecordContent.Builder> stateList;

    public SwapDeviceAdapter(Context context, List<RecordContent.Builder> stateList) {
        super(context);
        this.stateList = stateList;
    }

    @Override
    protected int onBindLayout() {
        return R.layout.item_swap_device_layout;
    }

    @Override
    protected ViewHolder onCreateHolder(View view) {
        ItemSwapDeviceLayoutBinding binding = ItemSwapDeviceLayoutBinding
                .inflate(LayoutInflater.from(mContext), (ViewGroup) view, false);
        return new ViewHolder(binding);
    }

    @Override
    protected void onBindData(ViewHolder holder, RecordContent item, int position) {

        holder.binding.title.setText(item.getContentName());
        holder.binding.value.setText(item.getContentValue());
        holder.binding.value.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    String value = s.toString().trim();
                    stateList.get(position).setContentValue(value);
                }
            }
        });
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private ItemSwapDeviceLayoutBinding binding;

        public ViewHolder(@NonNull ItemSwapDeviceLayoutBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }
    }
}
